package LeetCodeHot100TopInterview;

import java.util.Stack;

//最小栈
public class Q155_MinStack {
    private Stack<Integer> stack = null;
    private Stack<Integer> min = null;
    public Q155_MinStack() {
        stack = new Stack<>();
        min = new Stack<>();
    }

    public void push(int val) {
        stack.push(val);
        if (min.isEmpty() || val <= min.peek()){
            min.push(val);
        }
    }

    public void pop() {
        if (!stack.isEmpty()){
            Integer popVal = stack.pop();
            if (!min.isEmpty() && min.peek().equals(popVal)){
                min.pop();
            }
        }
    }

    public int top() {
        if (!stack.isEmpty()){
            return stack.peek();
        }
        return -1;
    }

    public int getMin() {
        if (!min.isEmpty()){
            return min.peek();
        }
        return -1;
    }

    public static void main(String[] args) {
        Q155_MinStack fun = new Q155_MinStack();
        fun.push(1);
        fun.push(2);
        fun.push(0);
        System.out.println("top" + fun.top());
        System.out.println("getmin" + fun.getMin());
        System.out.println("pop" + fun);
        fun.pop();
        System.out.println(fun.getMin());
        fun.pop();
        System.out.println(fun.getMin());
    }
}
